package com.zkyc.framework.website.config;

import com.zkyc.framework.common.constant.NumberConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Set;

/**
 * @author bing.bai
 */
@Configuration()
@Slf4j
public class JedisPoolFactory {

    private static final String SET_IF_NOT_EXIST = "NX";
    private static final String SET_WITH_EXPIRE_TIME = "PX";

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private Integer redisPort;

    @Value("${spring.redis.password}")
    private String redisPassword;

    private static volatile JedisPool jedisPool;

    public static String lock(String lockKey, String value, int i) {
        String result;
        try (Jedis resource = jedisPool.getResource()) {
            result = resource.set(lockKey, value, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, i * 1000);
        } catch (Exception e) {
            return "fail";
        }
        return result;
    }

    @Bean
    public JedisPool JedisPoolInit() {
        log.info("初始化jedis bean:" + redisHost + "redisPort:" + redisPort);
        //定义连接 池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxWaitMillis(1000);
        jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(1800000);
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setTestOnBorrow(true);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisPassword, 0);
        JedisPoolFactory.jedisPool = jedisPool;
        return jedisPool;
    }

    public static JedisPool getJedisPoolInstance() {
        return jedisPool;
    }

    public static String select(String key) {
        String result = null;
        try (Jedis resource = jedisPool.getResource()) {
            result = resource.get(key);
        } catch (Exception e) {
            log.info("jedispool获取出错", e);
        }
        return result;
    }

    public static void delete(String key) {
        try (Jedis resource = jedisPool.getResource()) {
            resource.del(key);
        } catch (Exception e) {
            log.info("jedis删除出错", e);
        }
    }

    public static void setex(String s, int daySecond, String toJSONString) {
        try (Jedis resource = jedisPool.getResource()) {
            resource.setex(s, daySecond, toJSONString);
        } catch (Exception e) {
            log.info("jedispool获取出错", e);
        }
    }

    public static void scanAndDel(String prefix) {
        prefix = prefix + "*";
        String[] strings = new String[0];
        try (Jedis resource = jedisPool.getResource()) {
            Set<String> keys = resource.keys(prefix);
            String[] keyArr = keys.toArray(strings);
            // 判空
            if (keyArr.length != NumberConstant.NUMBER_0) {
                resource.del(keyArr);
            }
        } catch (Exception e) {
            log.info("jedis扫描删除出错", e);
        }
    }

    public static void setnx(String s, String value) {
        try (Jedis resource = jedisPool.getResource()) {
            resource.setnx(s, value);
        } catch (Exception e) {
            log.info("jedispool获取出错", e);
        }
    }

    public static void incr(String key) {
        try (Jedis resource = jedisPool.getResource()) {
            resource.incr(key);
        } catch (Exception e) {
            log.info("jedispool获取出错", e);
        }
    }


}

